#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年10月12日

@author: chenyongbing
'''
import sys,os
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

'''
    根据 不同的手机系统 计算指标

'''


sys.path.append(os.path.join(current_dir,'../../'))
from base.zhimind_datas import myZhimindDatas
from base.retail_datas import myRetailDatas


#新客流入店率



#重复客流入店率
#驻店时长
#平均深访时长
#深访率
#跳出率
#新顾客占比
#老顾客占比
#老顾客中的高活跃度类型顾客占比（1-23）
#老顾客中的中活跃度类型顾客占比（24-30）
#老顾客中的低活跃度类型顾客占比（31－112）




def get_value_from_dict(dict1,key):
    if dict1.has_key(key):
        return dict1[key]
    else:
        return 0

def main(group='',startTime='',endTime = '',factory=None):
    group = int(group)
    
    shop_name = myRetailDatas.get_group_name(group=group)
    comp_name = myRetailDatas.get_comp_name(shop_group=group)
    print '%s - %s (%s)'%(comp_name,shop_name,group)
    
    for dis_factory,mac_type in [[None,'all'],['apple','global']]:
        
        if dis_factory=="all":
            print '	'.join(['指标','全部数据'])
        elif dis_factory=='apple':
            print '	'.join(['指标','安卓数据'])
        #新顾客人次
        new_visit_cnt_data = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group(group=group,distinct=False,role=3, st=startTime, et=endTime, factory=factory, dis_factory=dis_factory, mac_type=mac_type)
        
    
        
        #老顾客人次
        old_visit_cnt_data = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group(group=group,distinct=False,role=10, st=startTime, et=endTime, factory=factory, dis_factory=dis_factory, mac_type=mac_type)
        
        new_visit_cnt = get_value_from_dict(new_visit_cnt_data,group)
        old_visit_cnt = get_value_from_dict(old_visit_cnt_data,group)
        #print '新老人次',new_visit_cnt,old_visit_cnt
        
        #新顾客人数
        new_mac_cnt_data = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group(group=group,distinct=True,role=3, st=startTime, et=endTime, factory=factory, dis_factory=dis_factory, mac_type=mac_type)
        
        #老顾客人数
        old_mac_cnt_data = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group(group=group,distinct=True,role=10, st=startTime, et=endTime, factory=factory, dis_factory=dis_factory, mac_type=mac_type)
        new_mac_cnt = get_value_from_dict(new_mac_cnt_data,group)
     
        old_mac_cnt = get_value_from_dict(old_mac_cnt_data,group)
        #print '新老人数',new_mac_cnt,old_mac_cnt
        
        print '	'.join(['新顾客占比',str(round(float(new_mac_cnt)/(new_mac_cnt+old_mac_cnt),4))])
        print '	'.join(['老顾客占比',str(round(float(old_mac_cnt)/(new_mac_cnt+old_mac_cnt),4))])
        
        #深访人次
        deep_visit_cnt_data = myZhimindDatas.get_customer_count(groups=[group], startTime=startTime, endTime=endTime, customerType='deep', factory=factory, dis_factory=dis_factory, mac_type=mac_type)
        
        #跳出人次
        jump_visit_cnt_data = myZhimindDatas.get_customer_count(groups=[group], startTime=startTime, endTime=endTime, customerType='jump', factory=factory, dis_factory=dis_factory, mac_type=mac_type)
        
        deep_visit_cnt = int(get_value_from_dict(deep_visit_cnt_data,group))
        jump_visit_cnt = int(get_value_from_dict(jump_visit_cnt_data,group))
        #print '深访 跳出人次',deep_visit_cnt,jump_visit_cnt
        
        print '	'.join(['深访率',str(round(float(deep_visit_cnt)/(new_visit_cnt+old_visit_cnt),4))])
        print '	'.join(['跳出率',str(round(float(jump_visit_cnt)/(new_visit_cnt+old_visit_cnt),4))])
        #驻店时长
        
        avg_dur_data = myZhimindDatas.get_customer_dur_from_mac_customer_stats_by_group( groups=[group], groupType=4, st=startTime, et=endTime, factory=factory, dis_factory=dis_factory, mac_type=mac_type)
        avg_dur = get_value_from_dict(avg_dur_data,group)
        print '	'.join(['驻店时长','%s s'%str(int(avg_dur)/(new_visit_cnt+old_visit_cnt)/1000)])
        
        
        #平均深访 时长
        
        avg_deep_dur_data = myZhimindDatas.get_same_target_from_mac_customer_stats(group=group, groupType=4, st=startTime, et=endTime, role=0, targetList=['deep_visit_sum_dur'], factory=factory, dis_factory=dis_factory, mac_type=mac_type)
        avg_deep_dur = avg_deep_dur_data[group]['deep_visit_sum_dur']
    
        print '	'.join(['平均深访时长','%s s'%str(avg_deep_dur/deep_visit_cnt/1000)])
        
        
        high_active_customer_cnt_data = myZhimindDatas.get_customer_cnt_with_delta_day(groups=[group], groupType=4, delta_range=[1,23], st=startTime, et=endTime, factory=factory, dis_factory=dis_factory, mac_type=mac_type, distinct=True)
        middle_active_customer_cnt_data = myZhimindDatas.get_customer_cnt_with_delta_day(groups=[group], groupType=4, delta_range=[24,30], st=startTime, et=endTime, factory=factory, dis_factory=dis_factory, mac_type=mac_type, distinct=True)
        low_active_customer_cnt_data = myZhimindDatas.get_customer_cnt_with_delta_day(groups=[group], groupType=4, delta_range=[31,112], st=startTime, et=endTime, factory=factory, dis_factory=dis_factory, mac_type=mac_type, distinct=True)
        
        high_active_customer_cnt = get_value_from_dict(high_active_customer_cnt_data,group)
        middle_active_customer_cnt = get_value_from_dict(middle_active_customer_cnt_data,group)
        low_active_customer_cnt = get_value_from_dict(low_active_customer_cnt_data,group)
        
        
        print '	'.join(['高活跃度顾客占比',str(round(float(high_active_customer_cnt)/old_mac_cnt,4))])
        print '	'.join(['中活跃度顾客占比',str(round(float(middle_active_customer_cnt)/old_mac_cnt,4))])
        print '	'.join(['低活跃度顾客占比',str(round(float(low_active_customer_cnt)/old_mac_cnt,4))])

if __name__ == '__main__':
    import argparse,re,datetime,time
    
    parser = argparse.ArgumentParser(description='args')
    parser.add_argument('--group',metavar=u"group",default=None)
    parser.add_argument('--st',metavar=u"st",default=None)
    parser.add_argument('--et',metavar=u"et",default=None)
    args = parser.parse_args()
    group = args.group
    st = args.st
    et = args.et
    
    main(group=group,startTime=st,endTime=et)

